﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace theRift
{
    public enum MouseButtons
    {
        LeftButton,
        MiddleButton,
        RightButton,
        XButton1,
        XButton2
    }

    public class InputState : GameComponent
    {
        protected MouseState mouseState;
        protected MouseState oldMouseState;
        protected Vector2 mousePosition;

        // Check if the mouse button is down
        public bool IsMouseButtonDown(MouseButtons mb)
        {
            return IsMouseButtonState(mb, ButtonState.Pressed, 
                ref mouseState);
        }

        // Check if the mouse button is up
        public bool IsMouseButtonUp(MouseButtons mb)
        {
            return IsMouseButtonState(mb, ButtonState.Released, 
                ref mouseState);
        }

        // Check if the mouse button is currently down, but was up before!
        public bool IsMouseButtonPress(MouseButtons mb)
        {
            return (IsMouseButtonDown(mb) &&
                IsMouseButtonState(mb, ButtonState.Released, ref oldMouseState));
        }

        // Check if mouse button is currently up, but was down before!
        public bool IsMouseButtonRelease(MouseButtons mb)
        {
            return (IsMouseButtonUp(mb) &&
                IsMouseButtonState(mb, ButtonState.Pressed,
                ref oldMouseState));
        }

        // Buttonstate class, basically just pass on the mouse button, required
        // state to check for and the actual mouse state.
        private bool IsMouseButtonState(MouseButtons mb, ButtonState state, 
            ref MouseState mouseState)
        {
            switch (mb)
            {
                case MouseButtons.LeftButton:
                    return (mouseState.LeftButton == state);
                case MouseButtons.MiddleButton:
                    return (mouseState.MiddleButton == state);
                case MouseButtons.RightButton:
                    return (mouseState.RightButton == state);
                case MouseButtons.XButton1:
                    return (mouseState.XButton1 == state);
                case MouseButtons.XButton2:
                    return (mouseState.XButton2 == state);
            }

            return false;
        }

        // Normalized Delta ScrollWheelValue.
        public float ScrollWheelDelta
        {
            get
            {
                return MathHelper.Clamp(
                    (mouseState.ScrollWheelValue -
                     oldMouseState.ScrollWheelValue),
                     -1,
                     1);
            }
        }

        public InputState(Game game)
            : base(game)
        {
            oldMouseState = mouseState = Mouse.GetState();
            mousePosition = new Vector2(mouseState.X, mouseState.Y);
        }

        public override void Update(GameTime gameTime)
        {
            oldMouseState = mouseState;
            mouseState = Mouse.GetState();

            base.Update(gameTime);
        }
    }
}
